﻿Public Class Catalogo
    Dim resultado As Object = 0
    Dim dtLista As DataTable

    Public Function Mostrar(ByVal query As String, ByVal conexion As String, Optional ByVal seleccionado As Integer = 0, Optional ByVal filtro As Boolean = False) As Object
        Dim con As New Conexion_SQL(conexion)
        dtLista = New DataTable
        con.OpenQueryIntoDataTable(dtLista, query)
        lista.Items.Clear()

        Dim idSel As Integer = -1
        Dim i As Integer = 0
        For Each fila As DataRow In dtLista.Rows
            lista.Items.Add(fila(1).ToString)
            If fila(0).ToString = seleccionado.ToString Then
                idSel = i
            End If
            i += 1
        Next

        lista.SelectedIndex = idSel

        If filtro Then
            txtFiltro.Text = ""
            txtFiltro.Visible = True
            txtFiltro.Focus()
        Else
            txtFiltro.Visible = False
        End If

        Me.ShowDialog()

        Return resultado
    End Function

    Private Sub lista_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lista.KeyDown
        If lista.SelectedIndex >= 0 And (e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Return) Then
            resultado = dtLista.Rows(lista.SelectedIndex)(0)
            Me.Close()
        End If
        If e.KeyCode = Keys.Escape Then
            resultado = 0
            Me.Close()
        End If
    End Sub

    Private Sub lista_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lista.SelectedIndexChanged
        If lista.SelectedIndex >= 0 Then
            resultado = dtLista.Rows(lista.SelectedIndex)(0)
        Else
            resultado = 0
        End If
    End Sub

    Private Sub txtFiltro_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFiltro.TextChanged
        For Each fila As DataRow In dtLista.Rows

        Next
    End Sub
End Class